package com.py.payment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.py.payment.entity.PyInfoType;
import com.py.payment.entity.PyPayRecords;
import com.py.payment.entity.PyPayTitle;
import com.py.payment.service.PyInfoTypeService;
import com.py.payment.service.PyPayRecordsService;
import com.py.payment.service.PyPayTitleService;
import com.py.payment.tools.ToolsUtils;
import com.py.payment.tools.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/pay")
public class PayTitleController {

    @Autowired
    private PyPayTitleService payTitleService;

    @Autowired
    private PyPayRecordsService payRecordsService;

    @Autowired
    private PyInfoTypeService infoTypeService;

    @PostMapping(name = "新增支付台账标题",path = "/saveTile")
    public ResponseData saveTile(@RequestBody PyPayTitle payTitle){
        QueryWrapper<PyPayTitle> payTitleQueryWrapper = new QueryWrapper<>();
        payTitleQueryWrapper.eq("title", payTitle.getTitle());
        payTitleQueryWrapper.eq("type", payTitle.getType());
        List<PyPayTitle> list = this.payTitleService.list(payTitleQueryWrapper);
        if(ToolsUtils.isEmpty(list)){
            this.payTitleService.save(payTitle);
        }
        return ResponseData.success();
    }

    @PostMapping(name = "获取支付台账标题列表",path = "/getPayTitleList")
    public ResponseData getPayTitleList(@RequestBody PyPayTitle payTitle){
        QueryWrapper<PyPayTitle> payTitleQueryWrapper = new QueryWrapper<>();
        payTitleQueryWrapper.eq("type", payTitle.getType());
        List<PyPayTitle> list = this.payTitleService.list(payTitleQueryWrapper);
        return ResponseData.success(list);
    }

    @GetMapping("/test")
    public ResponseData test(){
        List<PyInfoType> infoTypeList = infoTypeService.list();
        for (PyInfoType infoType : infoTypeList) {
            QueryWrapper<PyPayRecords> payRecordsQueryWrapper = new QueryWrapper<>();
            payRecordsQueryWrapper.select("title");
            payRecordsQueryWrapper.eq("type",infoType.getId());
            payRecordsQueryWrapper.groupBy("title");
            List<PyPayRecords> payRecordsList = payRecordsService.list(payRecordsQueryWrapper);
            List<PyPayTitle> payTitleList = new ArrayList<>();
            payRecordsList.forEach(item->{
                PyPayTitle payTitle = new PyPayTitle();
                payTitle.setTitle(item.getTitle());
                payTitle.setType(infoType.getId());
                payTitleList.add(payTitle);
            });
            this.payTitleService.saveBatch(payTitleList);
            Map<String,PyPayTitle> map = new HashMap<>();
            payTitleList.forEach(item->{
                map.put(item.getTitle(),item);
            });
            payRecordsQueryWrapper = new QueryWrapper<>();
            payRecordsQueryWrapper.eq("type",infoType.getId());
            payRecordsList = this.payRecordsService.list(payRecordsQueryWrapper);
            List<PyPayRecords> payRecords = new ArrayList<>();
            payRecordsList.forEach(item->{
                PyPayRecords records = new PyPayRecords();
                PyPayTitle payTitle = map.get(item.getTitle());
                Integer titleId = payTitle.getId();
                records.setId(item.getId());
                records.setTitle(titleId.toString());
                payRecords.add(records);
            });
            this.payRecordsService.updateBatchById(payRecords);
        }
        return ResponseData.success();
    }
}
